//
//  HLHomeCellPicturesView.m
//  weibo-OC
//
//  Created by 黄力 on 15/12/21.
//  Copyright © 2015年 HLTest. All rights reserved.
//

#import "HLHomeCellPicturesView.h"
#import <Masonry.h>
#import "HLHomeCellPicturesViewCell.h"

//间距
#define pictureCellMargin 5
//重用标示
static NSString *pictureCellID = @"pictureCell";
@interface HLHomeCellPicturesView ()<UICollectionViewDataSource>

//记录约束
@property (nonatomic,strong) UICollectionViewFlowLayout *flowLayout;

@end

@implementation HLHomeCellPicturesView

//重写构造方法
- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {
    //创建约束对象
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout = flowLayout;
    //设置最小间距
    flowLayout.minimumInteritemSpacing = pictureCellMargin;
    flowLayout.minimumLineSpacing = pictureCellMargin;
    
    //创建
    self = [super initWithFrame:frame collectionViewLayout:flowLayout];
    
    self.backgroundColor = [UIColor whiteColor];
    //注册cell
    [self registerClass:[HLHomeCellPicturesViewCell class] forCellWithReuseIdentifier:pictureCellID];
    //设置数据源
    self.dataSource = self;
    
    return self;
}

//重写setter方法
- (void)setImageUrlArr:(NSArray *)imageUrlArr {
    _imageUrlArr = imageUrlArr;
    
    //设置约束
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo([self calculatePicturesViewSize]);
    }];
    //刷新界面
    [self reloadData];
}

//根据图片张数来计算尺寸
- (CGSize)calculatePicturesViewSize {
    //最大宽度
    CGFloat picturesViewMaxWidth = MainScreenWidth - 2 * HomeCellMargin;
    //单张图片最小宽度
    CGFloat cellMinWidth = (picturesViewMaxWidth - 2 * pictureCellMargin) / 3;
    
    //当没有图片时
    if (self.imageUrlArr == nil || self.imageUrlArr.count == 0) {
        
        return CGSizeZero;
    }else if (self.imageUrlArr.count == 1) {
        self.flowLayout.itemSize = CGSizeMake(150, 120);
        return CGSizeMake(150, 120);
    }else if (self.imageUrlArr.count == 4) {
        self.flowLayout.itemSize = CGSizeMake(cellMinWidth, cellMinWidth);
        return CGSizeMake(cellMinWidth * 2 + pictureCellMargin, cellMinWidth * 2 + pictureCellMargin);
    }else {
        self.flowLayout.itemSize = CGSizeMake(cellMinWidth, cellMinWidth);
        //计算行数
        NSInteger num = (self.imageUrlArr.count - 1) / 3 + 1;
        return CGSizeMake(picturesViewMaxWidth, cellMinWidth * num + (num - 1) * pictureCellMargin);
    }
}

#pragma mark - 数据源方法
//设置个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imageUrlArr.count;
}

//设置内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    //获取cell
    HLHomeCellPicturesViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:pictureCellID forIndexPath:indexPath];
    //设置数据
    cell.imageUrl = self.imageUrlArr[indexPath.item];
    return cell;
}

@end
